<?php

class yzImage extends yzCore{
	
	protected $aProperties = array(
	   'yz.image.default.memory' => 67102323,
	   'yz.image.formats'        => '',
	   'yz.image.jpeg_to_jpg'      => true,

	);
	
	protected $aErrors = array(
	   701 => 'No gd library!',
	   702 => 'Resource doesn\'t exists',
	   703 => 'Can\'t resize image',
	   704 => 'Unknown image type'
	);
	
	public function copyFromDbToFile($nId, $sFileName){}
	
	public function copyFromFileToDb($sFileName, $nId = null, $bUnlink = false){}
	
	public function getType($sImageType){
		
		$sImageType = strtolower(strval($sImageType));
		
		return (in_array($sImageType, $this->getProperty('yz.image.formats'))) ? $sImageType : null;

	}
	
	public function getExtension($sImageType){

		$sImageType = $this->gettype($sImageType);
		
		if ($sImageType=='jpeg' && $this->getProperty('yz.image.jpeg_to_jpg')){
				
				$sImageType = 'jpg';
				
		} // if
			
		return $sImageType;
	
	} // function
	
	public function loadFromFile($sFileName, $sImageType = null){
		
        if ($sImageType == null){
        	
        	$aImageInfo = getimagesize($sFileName);
        	
        	$sImageType = $this->yzImageFormats[$aImageInfo[2]];
        	
        } // if
        
        $sImageType = $this->getType($sImageType);
        
        if ($sType != null && file_exists($sFileName)){
        	
        	$sFunction = 'imagecreatefrom'.$sType;
    
        	$rImage = @$sFunction($sFileName);

	        if (is_resource($rImage) && get_resource_type($rImage)=='gd'){
	        	
	        	return $rImage;
	        	
	        } // if
        	
        } // if
		
		return null;	
		
	} // function
	
	public function send(){}
	
	public function loadFromDb($mId){
		
		$sQuery = $this->getObject('yzSql')->select($this->getProperty('yz.image.field.image'),$this->getProperty('yz.image.table'),array($this->getProperty('yz.image.field.id')=>intval($mId)));

		$this->getObject('__db')->query($sQuery);
		
		$aResult = $this->getObject('__db')->fetch();
		
		return $aResult[$this->getProperty('yz.image.field.image')];
		
		
		
		
	}
	
	public function saveToFile(&$rImage, $sFileName, $sFormat, $bAddExtension = true){
		
		try {
		
			$sImageType = $this->getType($sFormat);
			
			if ($sImageType!=null){
				
				$sFunction = 'image'.$sImageType;
				
				if (!function_exists($sFunction)){
					
					return false;
					
				} // if
				
				if ($bAddExtension == true) {
					
					$sFileName.=$this->getExtension($sImageType);
				
				} // if
				
				$bResult = $sFunction($rImage,$sFileName);
	
				if ($bResult == true){
					
					return $sFileName;
					
				} // if
		
			} else {
				
            throw new yzException($this->aErrors[704]);

			} // if
			
		} catch (Exception $oException){

			throw new yzException($oException->getMessage(),$oException->getCode());

		} // try
		
		return false;
		
	} // function save()
	
	public function resize(&$mImage, $nNewWidth = null, $nNewHeight = null, $bOnlyIfBigger =true){
		       
    try {	
	   
    	if (is_string($mImage) && file_exists($mImage)){
	   	
	       $rImage = $this->loadFromFile($mImage);	
	   	
	   } else if(is_numeric($mImage)){
	   	
	   	   $rImage = $this->loadFromDb($mImage);
	   	
	   } // if
	   
	   if (!is_resource($rImage) || get_resource_type($rImage)!='gd'){
	   	
	   	   throw new yzException($this->aErrors[702]);
	   	
	       return null;	
	   	
	   }

	   $nCurrentWidth = imagesx($rImage);	
		
       $nCurrentHeight = imagesy($rImage);   
       
       if ((int)$nNewWidth < 0){
       	
            $nNewWidth = $nCurrentWidth;	
 
       } // if

       if ((int)$nNewHeight < 0){
        
            $nNewHeight = $nCurrentHeight;   
 
       } // if      
       
       if ($bOnlyIfBigger == true){
       	
            $nNewWidth = min($nNewWidth,$nCurrentWidth);
            
            $nNewHeight = min($nNewHeight,$nCurrentHeight);
          	
       } // if

  
            $rNewImage = imagecreatetruecolor($nNewWidth, $nNewHeight);
    
            imagecopyresampled($rNewImage, $rImage, 0, 0, 0, 0, $nNewWidth, $nNewHeight, $nCurrentWidth, $nCurrentHeight);    

            return $rNewImage;

        } catch (Exception $oException) {
        	
        	throw new yzException($this->aErrors[703]);
        	
        } // try

        return null;
		
	}
	
    public function init(){
		
		if (!extension_loaded('gd')){
			
			throw new yzException($this->aErrors[701]);
			
			return false;

		} // if
		
		$this->setProperty('runtime.current_memory',ini_get('memory_limit'));
		
		if ($this->getProperty('yz.image.default.memory') > $this->getProperty('runtime.current_memory')){

			ini_set('memory_limit', $this->getProperty('yz.image.default.memory'));
			
		} // if
		
		$this->setObject('yzSql');
		
		if ($this->getProperty('yz.image.formats')==''){
			
			  $aImageTypes = array();
			
		      $nImageTypes = imagetypes();
		      
		      if ($nImageTypes & IMG_PNG)   { $aImageTypes[IMG_PNG] = 'png';   }
              if ($nImageTypes & IMG_GIF)   { $aImageTypes[IMG_GIF] = 'gif';   }
              if ($nImageTypes & IMG_JPG)   { $aImageTypes[IMG_JPG] = 'jpg';   }
              if ($nImageTypes & IMG_WBMP)  { $aImageTypes[IMG_WBMP] = 'wbmp';  }
              if ($nImageTypes & IMG_XPM)   { $aImageTypes[IMG_XPM] = 'xpm';   }
              
              if (empty($aImageTypes)){
              	
                throw new yzException($this->aError[701]);	
                
                return false;
              	
              } // if
              
              $this->setProperty('yz.image.formats', $aImageTypes);
  	
		} // if

	} // init
		
	public function __destruct(){
				
		if ($this->isSetProperty('runtime.current_memory') && ini_get('memory_limit')==$this->getProperty('yz.image.default.memory')){

			ini_set('memory_limit', $this->getProperty('runtime.current_memory'));
			
		} // if
		
		parent::__destruct();

	} // function destruct()
	
} // class


?>